<template>

  <div>
    <el-form ref="form" :model="form" label-width="80px">

    <div>
      <el-form-item label="字典名称">
        <el-input v-model="form.dicName"></el-input>
      </el-form-item>
      <el-form-item label="字典值">
        <el-input v-model="form.dicValue"></el-input>
      </el-form-item>
      <el-form-item label="备注">
        <el-input v-model="form.remark"></el-input>
      </el-form-item>
    </div>
    <div class="button" style="width:3%;float:right;">
      <P>
        <el-button class="el-icon-plus" @click.prevent="addRow()"></el-button>
      </P>
      <p>
        <el-button class="el-icon-minus" @click.prevent="delData()"></el-button>
      </p>
    </div>
    <div class="table">
      <el-table
        :data="tableData"
        ref="table"
        tooltip-effect="dark"
        border
        stripe
        style="width: 95%"
        @selection-change='selectRow'>
        <el-table-column type="selection" width="45" align="center"></el-table-column>
        <el-table-column label="序号" type="index" width="60" align="center"></el-table-column>
        <el-table-column label="字典子集名称" align="center">
          <template slot-scope="scope">
            <el-input v-model="scope.row.decName"></el-input>
          </template>
        </el-table-column>
        <el-table-column label="字典子集值">
          <template slot-scope="scope">
            <el-input v-model="scope.row.decValue"></el-input>
          </template>
        </el-table-column>
        <el-table-column label="图片">
          <template slot-scope="scope">
            <el-input v-model="scope.row.decImg"></el-input>
          </template>
        </el-table-column>
        <el-table-column prop="备注" label="天气">
          <template slot-scope="scope">
            <el-input v-model="scope.row.decRemarks"></el-input>
          </template>
        </el-table-column>

      </el-table>
    </div>
      <el-form-item>
        <el-button type="primary" @click="saveData">保存</el-button>
        <el-button>取消</el-button>
      </el-form-item>
    </el-form>
  </div>


</template>

<script>
  export default {
    name: "addDec",
    data() {
      return {
        tableData: [{
          decName:'',
          decValue:'',
          decImg:'',
          decRemarks:''

        }],
        form: {
          dicName: '',
          dicValue: '',
          remarks: ''
        },
        selectlistRow: []
      }
    },
    methods: {

      saveData() {
        var object = this.form
        object.decList=this.tableData;
        this.$post('/card/api/card/dec', this.$qs.parse(object))
          .then((response) => {
            console.log(response)
            this.$message('添加成功');
          })
          .catch(response => {
            console.log(response)
          })
      },
      // 获取表格选中时的数据
      selectRow(val) {
        this.selectlistRow = val
      },
      // 增加行
      addRow() {
        var list = {
          rowNum: '',
          address: this.address,
          name: this.name,
          weather: this.weather,
          phone: this.phone,
          date: this.date,
          mdate: this.mdate,
          loveer: this.loveer
        }
        this.tableData.push(list)
      },
      // 删除方法
      // 删除选中行
      delData() {
        for (let i = 0; i < this.selectlistRow.length; i++) {
          let val = this.selectlistRow
          // 获取选中行的索引的方法
          // 遍历表格中tableData数据和选中的val数据，比较它们的rowNum,相等则输出选中行的索引
          // rowNum的作用主要是为了让每一行有一个唯一的数据，方便比较，可以根据个人的开发需求从后台传入特定的数据
          val.forEach((val, index) => {
            this.tableData.forEach((v, i) => {
              if (val.rowNum === v.rowNum) {
                // i 为选中的索引
                this.tableData.splice(i, 1)
              }
            })
          })
        }
        // 删除完数据之后清除勾选框
        this.$refs.tableData.clearSelection()
      }
    }
  }
</script>

<style scoped>

</style>
